/******************************************************************************
 *                                                                            *
 *               (c) 2008-2009 HutLab. All rights reserved.                   *
 *                        Eduardo Henrique Leite                              *
 *                                                                            *
 ******************************************************************************

 History:
 Created 04/01/2009

 */

package hutlab.sudoku.engine.ui;

import hutlab.sudoku.engine.base.*;
import hutlab.sudoku.engine.*;
import javax.microedition.lcdui.*;
import java.util.*;

/**
 * Sudoku timer class.
 */
public class SudokuTime extends GameObject{

    // ----- FIELDS -----
    
    private SudokuGameCanvas mCanvas;
    private int mSeconds;
    private Timer mTimer;
    private TimerTask mTask;
    private ColourRGB mFontColour;
    private Font mFont;
    private boolean mActive;
    private boolean mAutoRefresh;
            
    // ----- GETTERS -----

    /**
     * Returns Canvas object.
     */
    public SudokuGameCanvas getCanvas(){
        return mCanvas;
    }

    /**
     * Returns the font colour.
     */
    public ColourRGB getFontColour(){
        return mFontColour;
    }

    /**
     * Returns the font.
     */
    public Font getFont(){
        return mFont;
    }

    /**
     * Returns whether the time is active or not.
     */
    public boolean getActive(){
        return mActive;
    }

    /**
     * Returns whether the screen will be automatically redrawn.
     */
    public boolean getAutoRefresh(){
        return mAutoRefresh;
    }

    // ----- SETTERS -----

    /**
     * Sets the font colour.
     */
    public void setFontColour(ColourRGB colour){
        mFontColour = colour;
    }

    /**
     * Sets the font.
     */
    public void setFont(Font font){
        mFont = font;
    }

    /**
     * Sets the AutoRefresh flag.
     */
    public void setAutoRefresh(boolean autoRefresh){
        mAutoRefresh = autoRefresh;
    }

    // ----- CONSTRUCTOR -----

    /**
     * Creates a new SudokuTime object.
     */
    public SudokuTime(SudokuGameCanvas canvas){
        this.mCanvas = canvas;
        this.mActive = false;
        this.mTimer = new Timer();
        this.mTask = new SudokuTimeTask();
        mTimer.schedule(this.mTask, 0, 1000);
    }

    // ----- PUBLIC -----

    /**
     * Starts the timer.
     */
    public void Start(){
        this.mActive = true;
    }

    /**
     * Stops the timer.
     */
    public void Stop(){
        this.mActive = false;
    }

    /**
     * GameObject render method override.
     */
    public void render(Graphics graphics) {
        graphics.setColor(this.mFontColour.getRed(),
                this.mFontColour.getGreen(),
                this.mFontColour.getBlue());
        graphics.setFont(this.mFont);
        graphics.drawString(transformSeconds(), getX(), getY(), Graphics.TOP | Graphics.RIGHT);
    }

    /**
     * GameObject keyCommand method override.
     */
    public int keyCommand(int keyCode) {
        return 0;
    }

    // ----- PRIVATE -----

    /**
     * Transforms seconds into a formatted string.
     */
    private String transformSeconds(){
        if (this.mSeconds <= 360){
            int minutes = this.mSeconds / 60;
            int seconds = this.mSeconds % 60;

            return String.valueOf(minutes) + ":" + String.valueOf(seconds);
        }
        return "";
    }

    /**
     * Timer task class.
     */
    private class SudokuTimeTask extends TimerTask{

        /**
         * Executed on timer's ticks.
         */
        public void run() {
            if (mActive){
                mSeconds++;
                
                if (mAutoRefresh){
                    mCanvas.repaint();
                }
            }
        }

    }
}
